/*
* To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.si3.controlador;


import java.util.HashMap;
import java.util.Map;

import com.si3.modelo.Profesor;
import com.si3.servicio.AddProfesorService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

/** 
 *
 * @author fjavieralba
 */
public class AddProfesorController extends SimpleFormController
{
    private AddProfesorService addProfesorService;

    public void setAddProfesorService(AddProfesorService addUserService)
    {
        this.addProfesorService = addUserService;
    }

    public AddProfesorService getAddProfesorService()
    {
        return addProfesorService;
    }
    
    
    public AddProfesorController()
    {
        setCommandClass(Profesor.class);
        setCommandName("profesor");
        setSuccessView("showMessage");
        setFormView("newProfesorForm");
    }
    
    
    @Override
    protected void initBinder(HttpServletRequest request,
            ServletRequestDataBinder binder) throws Exception {
        binder.registerCustomEditor(com.si3.modelo.Departamento.class, new DepartamentoEditorSupport());
        System.out.println("\t*** registered Centro property editor.");
    }

    
    protected Map referenceData(HttpServletRequest request, Object command, Errors errors) throws Exception 
    {
    	Map referenceData = new HashMap();
    	referenceData.put("departamentos", addProfesorService.getAllDepartamentos());
    	return referenceData;
      } 
    
    @Override
    protected ModelAndView onSubmit(HttpServletRequest request, 
                                    HttpServletResponse response, 
                                    Object command, 
                                    BindException errors) throws Exception 
    {
        ModelAndView mv = new ModelAndView(getSuccessView());
        
        Profesor profesor = (Profesor) command;
        addProfesorService.addNewProfesor(profesor);
        mv.addObject("message", "Nuevo profesor dado de alta");
        
        return mv;
    }
    
}
